package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import com.leyou.item.pojo.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping
    public ResponseEntity<Void> saveCart(@RequestBody Cart cart){
        this.cartService.saveCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts(){
        List<Cart> carts = this.cartService.queryCarts();
        if (CollectionUtils.isEmpty(carts)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(carts);
    }
    @DeleteMapping("delete/skuId")
    public ResponseEntity<Void>deleteCart(@PathVariable("skuId")Long skuId) {
        Boolean boo = cartService.deleteCart(skuId);
        if (boo) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.badRequest().build();
    }
   /* @GetMapping("collection")
    public ResponseEntity< List<Sku>> queryColl(){
        List<Sku> skus=cartService.queryColl();
        if (skus == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }
    @PostMapping("collection")
    public ResponseEntity<Void>addColl(@RequestParam("skuId")Long skuId){
        int i=cartService.addColl(skuId);
        if (i > 0) {
            return ResponseEntity.ok().build();
        }
        return ResponseEntity.badRequest().build();
    }*/
}
